Skip to content

fix #857 - fix on put when both @id and id are present#877

Merged
dunglas merged 1 commit into
api-platform:2.0from
Simperfit:fix/857-embedded
Jan 25, 2017
Merged

fix #857 - fix on put when both @id and id are present#877
dunglas merged 1 commit into
api-platform:2.0from
Simperfit:fix/857-embedded

Conversation

@Simperfit

Copy link
Copy Markdown
Contributor
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #857
License MIT
Doc PR

Add an non-regression test and modify the comportement of itemNormalizer

Comment thread src/Serializer/ItemNormalizer.php Outdated
{
// Avoid issues with proxies if we populated the object
if (isset($data['id']) && !isset($context['object_to_populate'])) {
if (isset($data['@id']) && !isset($context['object_to_populate'])) {

@dunglas dunglas Dec 10, 2016

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet already exists in the JSON-LD normalizer: https://github.com/api-platform/core/blob/master/src/JsonLd/Serializer/ItemNormalizer.php#L93

@id is specific to JSON-LD, so it should not be here. However id can contain an IRI too (when not using JSON-LD). So it should be kept. I guess it's a priority problem, in https://github.com/api-platform/core/blob/master/src/JsonLd/Serializer/ItemNormalizer.php#L101 the call to the parent method should be done before.

@Simperfit

Simperfit commented Dec 10, 2016

Copy link
Copy Markdown
Contributor Author

@dunglas This is now working but i'm not sure about the fix, since I don't think it's in the same scope then the iriConverter is, clearly this is not an IRI, do you have any thing in mind that would be better ?

EDIT: it's not the right fix ;)

@Simperfit Simperfit changed the title fix #857 - fix on put when both @id and id are present [WIP] fix #857 - fix on put when both @id and id are present Dec 10, 2016
@Simperfit Simperfit changed the title [WIP] fix #857 - fix on put when both @id and id are present fix #857 - fix on put when both @id and id are present Dec 10, 2016
@Simperfit

Copy link
Copy Markdown
Contributor Author

Again, maybe not the best fixes, but better than before since it's working and it does not break anything.

Comment thread src/Serializer/ItemNormalizer.php Outdated
}

$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
$context['object_to_populate'] = $this->iriConverter->getItemFromIri(is_int($data['id']) ? $this->iriConverter->getIriFromResourceClass($context['resource_class']).'/'.$data['id'] : $data['id'], $context + ['fetch_data' => false]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id can be from other types (uuid are strings for instance).

@Simperfit Simperfit Dec 13, 2016

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What could I do about that ?

Edit: Was thinking about try catching and doing the getIriFromResourceClass, WDYT @dunglas ?

@dunglas dunglas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When test will be green.

@theofidry theofidry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments for some nitpicking changes, 👍 overall :)

* @ApiResource(attributes={
* "normalization_context"={"groups"={"related_output", "output"}},
* "denormalization_context"={"groups"={"related_input", "input"}}
*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless extra line

/**
* @return int
*/
public function getId()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't a return typehint more suitable here and the other places in the same class?

Comment thread features/main/custom_normalized.feature Outdated
}
"""

Scenario: Create a resource with relation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the scenario summary (same for all below)...

@teohhanhui

teohhanhui commented Dec 20, 2016

Copy link
Copy Markdown
Contributor

The fix is simple: We must not support embedded relations with application/json, because the semantics are undefined. We have had this discussion before on Slack, and I thought a decision was made...

So I'm -1 since this is the wrong fix.

/cc @api-platform/core-team

@dunglas

dunglas commented Dec 20, 2016

Copy link
Copy Markdown
Member

@teohhanhui the issue here isn't with embedded relations but using raw ids instead of IRIs in raw JSON. Not sure we should support that too.

@teohhanhui

teohhanhui commented Dec 20, 2016

Copy link
Copy Markdown
Contributor

What is the issue with deserializing using a raw id property (or composite id represented as separate properties)? I think it should be supported.

@Simperfit

Copy link
Copy Markdown
Contributor Author

@teohhanhui updated

@dunglas dunglas added the bug label Dec 23, 2016
Comment thread src/Serializer/ItemNormalizer.php Outdated
try {
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
} catch (InvalidArgumentException $e) {
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($this->iriConverter->getIriFromResourceClass($context['resource_class']).'/'.$data['id'], $context + ['fetch_data' => false]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this going to work with composite identifiers?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also this is hardcoded to use the property "id"? 😢

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@teohhanhui @dunglas do we want to support composite identifiers ?

@Simperfit

Copy link
Copy Markdown
Contributor Author

ping @teohhanhui @dunglas modification done, WDYT ?

Comment thread src/Serializer/ItemNormalizer.php Outdated
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($data['id'], $context + ['fetch_data' => false]);
} catch (InvalidArgumentException $e) {
foreach ($this->propertyNameCollectionFactory->create($context['resource_class'], $context) as $propertyName) {
if (true === $this->propertyMetadataFactory->create($context['resource_class'], $propertyName)->isIdentifier()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't support yet composite identifiers, you can break after the first find. Or you can add support for composite identifiers :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for the composite support :D

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for now we should bug fix this and then add that feature ?

Comment thread src/Serializer/ItemNormalizer.php Outdated
$identifier = $propertyName;
}
}
$context['object_to_populate'] = $this->iriConverter->getItemFromIri($this->iriConverter->getIriFromResourceClass($context['resource_class']).'/'.$data[$identifier], $context + ['fetch_data' => false]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use sprintf here for clarity.

@dunglas

dunglas commented Jan 25, 2017

Copy link
Copy Markdown
Member

@api-platform/core-team is everyone OK to merge this one too? I want to publish a bugfix release today.

@dunglas dunglas merged commit 01303fa into api-platform:2.0 Jan 25, 2017
@dunglas

dunglas commented Jan 25, 2017

Copy link
Copy Markdown
Member

Thanks @Simperfit!

@Simperfit Simperfit deleted the fix/857-embedded branch January 25, 2017 15:01
magarzon pushed a commit to magarzon/core that referenced this pull request Feb 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants